package org.swxjava.servlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.swxjava.Container;
import org.swxjava.WebContext;
import org.swxjava.WebContextFactory.WebContextBuilder;
import org.swxjava.util.Log;
import org.swxjava.util.LogFactory;

public class DefaultWebContextBuilder implements WebContextBuilder {

	/** Logger */
	private static final Log LOG = LogFactory
			.getLog(DefaultWebContextBuilder.class);

	private static ThreadLocal thread = new ThreadLocal();

	public void set(HttpServletRequest request, HttpServletResponse response,
			ServletConfig config, ServletContext context, Container container) {
		try {
			WebContext ec = new DefaultWebContext(request, response, config,
					context, container);
			thread.set(ec);
		} catch (Exception ex) {
			LOG.fatal("Failed to create an ExecutionContext", ex);
		}
	}

	public WebContext get() {
		return (WebContext) thread.get();
	}

	public void unset() {
		thread.set(null);
	}
}